package 力扣_算法题综合.高频面试算法;

import java.util.ArrayList;
import java.util.List;

public class Z字型变换_6 {
    public String convert(String s, int numRows) {
        if(numRows < 2) return s;
        List<StringBuilder> list = new ArrayList<>();
        for(int i = 0;i < numRows;i++){
            list.add(new StringBuilder());
        }
        int k = 0;
        boolean flag = false;
        for(int i = 0;i < s.length();i++){
            if(k ==0 || k == numRows - 1){
                flag = !flag;
            }
            list.get(k).append(s.charAt(i));
            if(flag){
                k++;
            }else{
                k--;
            }
        }
        StringBuilder sb = new StringBuilder();
        for(int i = 0;i < list.size();i++){
            sb.append(list.get(i));
        }
        return sb.toString();
    }
}
